import {
  Toast
} from 'vant'
const state = {
  cartList: JSON.parse(localStorage.cartList || '[]'),
  checkBtn: true,
  orderList: JSON.parse(localStorage.orderList || '[]')
}

const mutations = {
  SET_CART_LIST(state, payload) {
    // 1) 判断是否第一次添加
    let flag = false
    state.cartList.forEach(item => {
      if (item.id === payload.id) {
        item.count += payload.count
        flag = true
      }
    })
    if (!flag) {
      state.cartList.push(payload)
    }
    // 2) 把数据存到本地
    localStorage.cartList = JSON.stringify(state.cartList)
  },
  // 全选按钮初始状态
  SET_ALL_CHECK(state, payload) {
    state.checkBtn = payload
  },
  SET_GOODS_STATE(state, payload) {
    // 全选按钮控制单个按钮
    payload = !payload
    state.checkBtn = payload
    state.cartList.forEach(item => (item.isCompleted = payload))
  },
  SET_BTN_CHECK(state, payload) {
    // 控制单个按钮
    state.cartList.some(item => {
      if (item.id === Number(payload)) {
        item.isCompleted = !item.isCompleted
      }
    })
    // 控制全选按钮
    state.checkBtn = state.cartList.every(item => item.isCompleted)
  },
  DEL_GOODS(state, payload) {
    const curId = state.cartList.findIndex(item => (item.id === Number(payload)))
    state.cartList.splice(curId, 1)
    localStorage.cartList = JSON.stringify(state.cartList)
  },
  SET_GOODS_ORDER(state) {
    // 注意: 要先判断一下是否勾选了商品
    if (state.cartList.every(item => !item.isCompleted)) return Toast('您还未选择待下单的商品')
    // 添加至订单
    const order = state.cartList.filter(item => (item.isCompleted))
    order.forEach(item => (item.add_time = Date.now()))
    state.orderList.unshift(...order)
    localStorage.orderList = JSON.stringify(state.orderList)

    // 更新购物车
    const ids = order.reduce((pre, cur) => {
      pre.push(cur.id)
      return pre
    }, [])

    ids.forEach(id => {
      const fIndex = state.cartList.findIndex((item, index) => (item.id === id))
      if (fIndex !== -1) state.cartList.splice(fIndex, 1)
    })

    localStorage.cartList = JSON.stringify(state.cartList)
    Toast('已添加至我的订单')
  },
  ADD_ORDER_ADDRESS(state, payload) {
    state.orderList.some((item, index) => {
      // 1) 更新订单信息
      if (item.id === payload.gid) {
        item.address = payload.address
        item.name = payload.name
        item.tel = payload.tel
        item.isCompleted_time = Date.now()
        item.orderId = Math.random().toString().split('.')[1]
      }
    })
    localStorage.orderList = JSON.stringify(state.orderList)
    Toast('已成功添加收货地址')
  },
  CANCEL_ORDER(state, payload) {
    const curId = state.orderList.findIndex(item => (item.id === payload))
    state.orderList.splice(curId, 1)
    localStorage.orderList = JSON.stringify(state.orderList)
    Toast('取消成功')
  },
  SUBMIT_ORDER(state, payload) {
    // 1) 先判断当前商品是否添加了地址
    const curOrder = state.orderList.find(item => (item.id === payload))
    if (curOrder.address) {
      // 2) 如果有地址就给当前商品添加属性isPay=true
      const curId = state.orderList.findIndex(item => (item.id === payload))
      state.orderList[curId].isPay = true
      localStorage.orderList = JSON.stringify(state.orderList)
      Toast('支付完成')
      location.reload(true) // 强制刷新
    } else {
      Toast('前先添加收货地址')
    }
  }
}

const actions = {

}

const getters = {
  getAllCheck(state) {
    return state.cartList.every(item => item.isCompleted)
  },
  getAllCartCount(state) {
    return state.cartList.length
  },
  getCheckCartPrice(state) {
    const price = state.cartList.reduce((pre, cur) => {
      if (cur.isCompleted) pre += cur.count * cur.price
      return pre
    }, 0)
    return price
  },
  getUnfinishedOrder(state) {
    return state.orderList.reduce((pre, cur) => {
      if (!cur.isPay) {
        pre++
      }
      return pre
    }, 0)
  },
  getFinishedOrder(state) {
    return state.orderList.reduce((pre, cur) => {
      if (cur.isPay) {
        pre++
      }
      return pre
    }, 0)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
